<?php
class ome_ctl_admin_stock extends desktop_controller{
    var $name = "库存查看";
    var $workground = "storage_center";

    function index(){
        
        /*
         * 获取操作员管辖仓库
         */
        $oBranch = &app::get('ome')->model('branch');
        $is_super = kernel::single('desktop_user')->is_super();
        $branch_ids = $oBranch->getBranchByUser(true);
        if (!$is_super){
            $branch_ids = $oBranch->getBranchByUser(true);
            if ($branch_ids){
                $filter['branch_id'] = $branch_ids;
            }else{
                $filter['product_id'] = 'false';
            }
        }
        $filter['product_group'] = true;
        
        $actions = array(
            array(
                'label' => '批量设置安全库存',
                'href'=>'index.php?app=ome&ctl=admin_stock&act=batch_safe_store',
                'target' => "dialog::{width:700,height:400,title:'批量设置安全库存'}",
              )
        );
        
        
       $this->finder('ome_mdl_products',array(
            'title'=>'库存查看',
            'base_filter' => $filter,
            'actions' => $actions,
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag'=>false,
            'use_buildin_recycle'=>false,
            'use_buildin_export'=>false,
            'use_buildin_import'=>false,
            'use_buildin_filter'=>true,
            'object_method'=>array('count'=>'countAnother','getlist'=>'getListAnother')
            
        ));
    }
    
    function batch_safe_store() {
        
        if($_POST) {
            $filter['branch_id'] = intval($_POST['branch']);
            $init_all = intval($_POST['init_all']);
            $safe_store = intval($_POST['safe_store']);
            $last_modified = time();
            if($init_all != 1) {
                $filter['safe_store'] = 0;
            }
            $obj = &app::get('ome')->model('branch_product');
            $obj -> update(array('safe_store'=>$safe_store,'last_modified'=>$last_modified),$filter);
            
            echo "<script>$$('.dialog').getLast().retrieve('instance').close();</script>";
            //return false;            
        }
        
        $suObj = &app::get('purchase')->model('supplier');
        $data  = $suObj->getList('supplier_id, name','',0,-1);

        $brObj = &app::get('ome')->model('branch');
        $row   = $brObj->getList('branch_id, name','',0,-1);

        // 获取操作员管辖仓库
        $is_super = kernel::single('desktop_user')->is_super();
        if (!$is_super){
           $branch_list = $brObj->getBranchByUser();
        }
        $this->pagedata['branch_list']   = $branch_list;
        $is_super = 1;
        $this->pagedata['is_super']   = $is_super;
        
        $this->pagedata['supplier'] = $data;
        $operator = kernel::single('desktop_user')->get_name();
        $this->pagedata['operator'] = $operator;

        $this->pagedata['branch']   = $row;
        $this->pagedata['branchid']   = $branch_id;
        $this->pagedata['cur_date'] = date('Ymd',time()).$order_label;
        $this->pagedata['io'] = $io;
        
        $this->display("admin/stock/batch_safe_store.html");        
    }

    /*详情
     *ss备注：货位相关方法，可以删除此方法，同时可以删除页面'admin/stock/edit_stock.html','admin/stock/edit_stocks.html'
     */
    function edit($product_id=0){
       $oBranch_product_pos = &$this->app->model("branch_product_pos");
       if($_POST){
           $this->begin('index.php?app=ome&ctl=admin_stock&act=edit&p[0]='.$_POST['product_id']);
           $product_id = $_POST['product_id'];
           $branch_id = $_POST['branch_id'];
           $pos_id = $_POST['pos_id'];
           //if($oBranch_product_pos->get_branch_pos_exist($product_id,$pos_id)>0){
               //$this->end(false, app::get('base')->_('此货品已和此货位建立关联'));
           //}
           $oBranch_product_pos->create_branch_pos($product_id,$branch_id,$pos_id);
           $this->end(true, app::get('base')->_('关联成功'));
       }

        $oProduct = &$this->app->model('products');
        $oBranch = &$this->app->model("branch");
       $branch_list=$oBranch->Get_branchlist();
    
       $oPos = &$this->app->model("branch_pos");
       $pos = $oPos->select('*');
       $this->pagedata['branch_list'] = $branch_list;
       $this->pagedata['pos'] = $pos;
       $this->pagedata['product_id'] = $product_id;
        $this->pagedata['pro_detail'] = $oProduct->products_detail($product_id);
        $this->page("admin/stock/edit_stock.html");
    }

    /*
     *ss备注：货位相关方法，可以删除此方法
     */
    function dosave(){
        $this->begin('index.php?app=ome&ctl=admin_stock&act=edit&p[0]='.$_POST['product_id']);
        $oProduct = &$this->app->model('products');
       
        $oProduct_pos = &app::get('ome')->model("branch_product_pos");
        $do_action = $_POST['do_action'];
        $ckid = $_POST['ckid'];
        $store = $_POST['store'];
        $branch_id = $_POST['branch_id'];
        $product_id = $_POST['product_id'];
        
        if($do_action=='save_branch'){
            foreach($ckid as $k=>$v){
              $adata = array('branch_id'=>$_POST['branch_id'][$v],'store'=>$_POST['store'][$v],'product_id'=>$_POST['product_id'],'pos_id'=>$v);
              //$total_store += $_POST['store'][$v];
              //$oProduct->tosave($adata);
              $oProduct_pos->change_store($_POST['branch_id'][$v], $_POST['product_id'], $v, $_POST['store'][$v], '=');
            }
            
            //更新商品总库存
            //$oProduct->chg_product_store($product_id, $total_store, '=');
 
             $this->end(true, app::get('base')->_('保存成功'));
        }else if($do_action=='reset_branch'){
         
            $oBranch_product_pos = &$this->app->model("branch_product_pos");
            $oBranch_product = &$this->app->model("branch_product");
           
                $pro = $oBranch_product_pos->dump(array('product_id'=>$product_id,'pos_id'=>$_POST['repos_id']),'store');
                if($pro['store']>0){
                  $this->end(false, app::get('base')->_('库存量大于0,不可以重置'));
                }
                /*判断仓库对应几个货位。货位是否大于1*/
                $assign = $oProduct_pos->get_pos($product_id,$_POST['rebranch_id']);
                
                $arrive = $oBranch_product->dump(array('product_id'=>$product_id,'branch_id'=>$_POST['rebranch_id']),'arrive_store');
                if($arrive['arrive_store']>0){
                    if(count($assign)==1){
                        $this->end(false, app::get('base')->_('不可重置在途库存大于0的最后一个货位'));
                    }
                }
               
            $oBranch_product_pos->reset_branch_pos($product_id,$_POST['rebranch_id'],$_POST['repos_id']);
            $this->end(true, app::get('base')->_('重置成功'));
           
        }else{
            $this->end(false, app::get('base')->_('不明参数。。。。'));
        }
    }

    /*
     * ss备注：货位相关方法，可以删除
     */
    function get_op($branch_id,$ajax='false'){
        $oBranch = &$this->app->model('branch');
        //$Pos = $oBranch->Get_poslist($branch_id);
        $oProduct_pos = &app::get('ome')->model("branch_product_pos");
        $Pos =$oProduct_pos->get_unassign_pos($branch_id);

        $branch_name=$oBranch->Get_name($branch_id);
        if($ajax == 'true'){
            $options = "<option value=''>请选择</option>";
            if($Pos && is_array($Pos)){
                foreach($Pos as $v){
                    $options .= "<option value=".$v['pos_id'].">".$v['store_position']."</option>";
                }
            }
            echo $options."</select>";
        }else{

        }
    }
    
    /*
     * 获取货位JSON
     * ss备注：货位相关方法，可以删除此方法
     */
    function getPosByBranchProduct(){
        $branch_id = $_GET['branch_id'];
        $pos_name = $_GET['store_position'];
        $product_id = $_POST['product_id'];
        $oProduct_pos = &app::get('ome')->model("branch_product_pos");
        if ($pos_name){
            //获取未与货品关联的货位
            //$Pos = $oProduct_pos->getPosByBranchProduct($branch_id,$product_id,$pos_name);
            //获取所有货位
            $Pos = $oProduct_pos->getPosByName($branch_id, $pos_name);
        }

        echo "window.autocompleter_json=".json_encode($Pos);
    }
    
    /*
     * 获取货位名称
     * getPosNameById
     * param id
     * ss备注：货位相关方法，可以删除此方法
     */
     function getPosNameById(){
        $id = $_POST['id'];
        $oBranchPos = &app::get('ome')->model('branch_pos');
        $branchpos = $oBranchPos->dump(array('pos_id'=>$id), 'pos_id,store_position');
        $tmp['id'] = $branchpos['pos_id'];
        $tmp['name'] = $branchpos['store_position'];
        echo json_encode($tmp);
        //echo "{'id':'".$branchpos['pos_id']."','name':'".$branchpos['store_position']."'}";
     }
         
    /*
     * 关联货位，全部展示
     * getPosFinder
     * ss备注：货位相关方法，可以删除此方法
     */
    function view($branch_id=null){

        $branch_id = intval($branch_id);
        $oBranch = &app::get('ome')->model('branch');
        $is_super = kernel::single('desktop_user')->is_super();
        
        $branch_mode = &app::get('ome')->getConf('ome.branch.mode');
        if ($branch_mode=='single'){
            $cols = 'store_position,column_product_bn,column_product_name';
        }else{
            $cols = 'store_position,branch_id,column_product_bn,column_product_name';
        }
        
        $params = array(
                        'title'=>'货位',
                        'use_buildin_new_dialog' => false,
                        'use_buildin_set_tag'=>false,
                        'use_buildin_recycle'=>false,
                        'use_buildin_export'=>false,
                        'use_buildin_import'=>false,
                        'use_buildin_filter'=>true,
                        'finder_aliasname'=>'search_branch_pos_finder',
                        'finder_cols'=>$cols,
                        'orderBy' => 'p.bn asc ',
                        'object_method' => array(
                            'count'=>'finder_count',   //获取数量的方法名
                            'getlist'=>'finder_list',   //获取列表的方法名
                        ),
                    );
        
        /*
         * 获取操作员管辖仓库
         */
        if (!$is_super){
            $branch_ids = $oBranch->getBranchByUser(true);
            if ($branch_ids){
                $params['base_filter']['branch_id'] = $branch_ids;
            }else{
                $params['base_filter']['branch_id'] = 'false';
            }
        }
       
        if($branch_id){
          if (!$is_super){
             if (in_array($branch_id,$branch_ids)){
               $params['base_filter']['branch_id'] = $branch_id;
             }else{
               $params['base_filter']['branch_id'] = '-';
             }
          }else{
               $params['base_filter']['branch_id'] = $branch_id;
          }
        }
        
        $this->finder('ome_mdl_branch_pos', $params);
    }

    /*
     * ss备注：货位相关方法，可以删除
     */
    function get_op1($branch_id,$ajax='false',$type,$product_id=''){
        $oBranch = &$this->app->model('branch');
        $Pos = $oBranch->Get_poslist($branch_id);
       
        $oProduct_pos = &$this->app->model('branch_product_pos');
        $branch_name=$oBranch->Get_name($branch_id);


        if($ajax == 'true'){
            if($type=="from"){
                    //建立关联货位
                    $unpos_list = $oProduct_pos->get_pos($product_id,$branch_id);
                    //未建立关联的货位
                    $pos_list = $oProduct_pos->get_unassign_pos($branch_id);
                    //全部货位
                    $formpos = array_merge($unpos_list,$pos_list);
                    $options = "<select id=from_pos_id name=from_pos_id>";
                    
                    if($unpos_list && is_array($unpos_list)){
                        foreach($unpos_list as $v){
                            $options .= "<option value=".$v['pos_id'].">".$v['store_position']."</option>";
                        }
                    }
             }else{
                 
                    //调入货位由所有货位改为读取与商品关联货位
                    //建立关联货位
                    $unpos_list = $oProduct_pos->get_pos($product_id,$branch_id);
                    //未建立关联的货位
                    //全部货位
                    $options = "<select id=to_pos_id name=to_pos_id>";
                    $options .= "<option value=''>选择</option>";
                    if($unpos_list && is_array($unpos_list)){
                        foreach($unpos_list as $v){
                            $options .= "<option value=".$v['store_position'].">".$v['store_position']."</option>";
                        }
                    }
                 
                    /*
                     * 所有货位
                    $options = "<select id=to_pos_id name=to_pos_id><option value=''>请选择</option>";
                    if($Pos && is_array($Pos)){
                        foreach($Pos as $v){
                            $options .= "<option value=".$v['store_position'].">".$v['store_position']."</option>";
                        }
                    }
                    */
             }

            $options.="</select>";
                 if($type=="from"){

                    $options.="<input type=hidden id=from_branch_name name=from_branch_name value=".$branch_name.">";
                }else{
                    $options.="<input type=hidden id=to_branch_name name=to_branch_name value=".$branch_name.">";
                }
                echo $options;
        }else{

        }
    }

    /*
     * 关联货位
     * ss备注：货位相关方法，可以删除此方法，同时可删除页面：admin/stock/change_stock.html
     */
    function change_pos($product_id){

       $oBranch = &$this->app->model("branch");
       $branch_list=$oBranch->Get_branchlist();
       $this->pagedata['branch_list'] = $branch_list;
       $this->pagedata['product_id'] = $product_id;
       
       //获取仓库模式
       //$branch_mode = &app::get('ome')->getConf('ome.branch.mode');
       //$this->pagedata['branch_mode'] = $branch_mode;
       
       /*
         * 获取操作员管辖仓库
         */
        $is_super = kernel::single('desktop_user')->is_super();
        if (!$is_super){
           $branch_list_byuser = $oBranch->getBranchByUser();
        }
        $this->pagedata['branch_list_byuser']   = $branch_list_byuser;
        $is_super = 1;
        $this->pagedata['is_super']   = $is_super;
       
        $this->display("admin/stock/change_stock.html");
    }
    /*
     * ss备注：货位相关方法，可以删除此方法
     */
    function create_pos(){
       $oBranch_product_pos = &$this->app->model("branch_product_pos");
       $this->begin('index.php?app=ome&ctl=admin_stock&act=edit&p[0]='.$_POST['product_id']);
       $product_id = $_POST['product_id'];
       $branch_id = $_POST['branch_id'];
       $pos_id = $_POST['pos_id'];
       $pos_name = $_POST['pos_name'];
       $oBranch_pos = app::get('ome')->model("branch_pos");
       //判断货位是否存在
       $branch_pos = $oBranch_pos->dump(array('store_position'=>$pos_name,'branch_id'=>$branch_id), 'pos_id');
       if (!$branch_pos){
           $this->end(false, $pos_name.'货位不存在');
       }
       $pos_id = $branch_pos['pos_id'];
       //f($oBranch_product_pos->get_branch_pos_exist($product_id,$pos_id,$branch_id)>0){
       //if($oBranch_product_pos->get_branch_pos_exist($product_id,$branch_id)>0){
           //$this->end(false, app::get('base')->_('此商品已和此仓库建立过关联'));
       //}
       if ($oBranch_product_pos->dump(array('product_id'=>$product_id,'pos_id'=>$pos_id), 'pos_id')){
           $this->end(false, $pos_name.'货位已与此货品关联。');
       }
       $oBranch_product_pos->create_branch_pos($product_id,$branch_id,$pos_id);
       $this->end(true, app::get('base')->_('关联成功'));
    }

    /*
     * ss备注：库存查询相关方法，可以删除
     */
    function search(){
        if($_POST['stock_search']){
            $data = $this->app->model('products')->search_stockinfo(addslashes(trim($_POST['stock_search'])));
            $this->pagedata['data'] = $data;
            $this->pagedata['keywords'] = $_POST['stock_search'];
        }
        $this->page("admin/stock/search.html");
    }
    
    /*
     * 货号及名称 自动填充
     */
    function getProductsByAuto(){
        
        $keywords = trim($_GET['stock_search']);
        if($keywords){
           $filter = array(
               'bn'=>$keywords
           );
           $pObj = &app::get('ome')->model('products');
           $pObj->filter_use_like = true;
           $data_ini = $pObj->getList('bn',$filter,0,-1);
           $data = array();
           if ($data_ini)
           foreach($data_ini as $k=>$v){
               $v['stock_search'] = $v['bn'];
               unset($v['name']);
               $data[] = $v;
           }
           
           $filter = array(
               'name'=>$keywords
           );
           $data_ini = $pObj->getList('name',$filter,0,-1);
           $pObj->filter_use_like = false;
           if ($data_ini)
           foreach($data_ini as $k=>$v){
               $v['stock_search'] = $v['name'];
               unset($v['name']);
               $data[] = $v;
           }
           //$data = array_unique($data);
        }
        echo "window.autocompleter_json=".json_encode($data);
    }
    
    function get_pos_store($product_id,$pos_id){
        $oBranchPorductPos = &$this->app->model('branch_product_pos');
        $pos_store = $oBranchPorductPos->dump(array('product_id'=>$product_id,'pos_id'=>$pos_id),'store');
        
        if($pos_store){
            echo $pos_store['store'];
        }else{
            echo 0;
        }
    }
    
    function checkPos($branch_id,$pos_name){
        $oBranchPos = &$this->app->model('branch_pos');
        $branch_pos = $oBranchPos->dump(array('branch_id'=>$branch_id,'store_position'=>$pos_name),'pos_id');
        
        if($branch_pos){
            echo $branch_pos['pos_id'];
        }else{
            echo 0;
        }
    }
    /**
     * 显示冻结库存的详情
     */
    function show_store_freeze_list(){
        $page = $_GET['page'] ? $_GET['page'] : 1;
        $pagelimit = 10;
        $store_freeze_num = $_GET['store_freeze_num'];
        $product_id = $_GET['product_id'];  
        $oiObj = $this->app->model('order_items');
        $offset = ($page-1)*$pagelimit;
        $store_freeze = $oiObj->getStoreByProductId($product_id,$offset,$pagelimit);
        $count = $oiObj->count_order_id($product_id);
        $total_page = ceil($count/$pagelimit);
        $pager = $this->ui()->pager(array(
            'current'=>$page,
            'total'=>$total_page,
            'link'=>'index.php?app=ome&ctl=admin_stock&act=show_store_freeze_list&store_freeze_num='.$store_freeze_num.'&product_id='.$product_id.'&target=container&page=%d',
        ));
        foreach($store_freeze as $k=>$v){
            $rows['nums'] = $v['nums'];
            $rows['sendnum'] = $v['sendnum'];   
            $rows['status'] = $v['ship_status'];    
            switch($rows['status'])
            {
                case 0:
                    $rows['status'] = '未发货';
                break;  
                case 1:
                    $rows['status'] = '已发货';
                break;  
                case 2:
                    $rows['status'] = '部分发货';
                break;  
                case 3:
                    $rows['status'] = '部分退货';
                break;  
                case 4:
                    $rows['status'] = '已退货';
                break;  
            }           
            $rows['order_bn'] = $v['order_bn'];
            $row[] = $rows;
        }
        $this->pagedata['rows'] = $row;
        $this->pagedata['pager'] = $pager;
        $this->pagedata['store_freeze_num'] = $store_freeze_num;
        if($_GET['target']){
        	return $this->display('admin/stock/freeze.html');
        }
        $this->singlepage('admin/stock/freeze.html');
        
    }
}
?>
